<template>
  <div class="comments">
    <div class="total" v-if="productDetailStore.comments.length">共{{ productDetailStore.comments.length }}条评论</div>
    <!-- 有评论 -->
    <div class="animate__animated animate__fadeIn" v-if="productDetailStore.comments.length">
      <ul>
        <li v-for="comment in productDetailStore.comments" :key="comment.id" class="comment-item">
          <!-- 渲染一级评论 -->
          <div class="comment-main">
            <img :src="comment.pubAvatar" alt="头像" class="avatar" />
            <div class="comment-content">
              <!-- 昵称和评论 -->
              <div class="name-content">
                <p class="nickname">{{ comment.pubNickname }}</p>
                <p class="content">{{ comment.content }}</p>
              </div>
              <!-- 时间和定位 -->
              <div class="meta">
                <p>{{ formatDateTime(comment.createTime) }}</p>
                <p style="margin-left: 5px;">{{ comment.pubProvince === undefined ? '未知 ip' : comment.pubProvince }}</p>
                <!-- 回复 -->
                <p class="reply" @click="useProductDetailStore().isReply = true,
                  useProductDetailStore().replyUserNickname = comment.pubNickname,
                  useProductDetailStore().commentDto.parentId = comment.id">
                  回复</p>
              </div>
            </div>
          </div>

          <!-- 渲染二级评论 -->
          <ul v-if="comment.children.length" class="children">
            <li v-for="child in comment.children" :key="child.id" class="comment-item child-comment">
              <div class="comment-main">
                <img :src="child.pubAvatar" alt="头像" class="avatar" />
                <div class="comment-content">
                  <!-- 昵称和评论 -->
                  <div class="name-content">
                    <p class="nickname">{{ child.pubNickname }}</p>
                    <p class="content">回复 <span style="font-size: 14px;font-weight: 600;color: rgba(51, 51, 51, 0.5);">
                        {{ child.parentNickname }}</span>
                      :{{ child.content }}
                    </p>
                  </div>
                  <!-- 时间和定位 -->
                  <div class="meta">
                    <p>{{ formatDateTime(child.createTime) }}</p>
                    <p style="margin-left: 5px;">{{ child.pubProvince === undefined ? '未知 ip' : child.pubProvince }}</p>
                    <!-- 回复 -->
                    <p class="reply" @click="useProductDetailStore().isReply = true,
                      useProductDetailStore().replyUserNickname = child.pubNickname,
                      useProductDetailStore().commentDto.parentId = child.id">
                      回复</p>
                  </div>
                </div>
              </div>
            </li>
          </ul>
        </li>
      </ul>
    </div>
    <!-- 空评论 -->
    <CommentEmpty v-else />
  </div>
</template>

<script setup lang='ts'>
import CommentEmpty from '../empty/CommentEmpty.vue'
import { useProductDetailStore } from '@/stores'
import { formatDateTime } from '@/utils/formatDate'
const productDetailStore = useProductDetailStore()
</script>

<style lang="less" scoped>
.comments {
  min-height: 380px;
  padding: 0px 20px 50px 20px;

  .total {
    font-size: 14px;
    color: rgba(51, 51, 51, 0.6);
    margin-bottom: 20px;


  }

  .comment-item {
    margin-bottom: 20px;

    .comment-main {
      display: flex;
      align-items: flex-start;
      margin-bottom: 10px;

      .avatar {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        margin-right: 10px;
      }

      .comment-content {
        flex: 1;

        .name-content {
          height: auto;
          min-height: 40px;
          display: flex;
          flex-direction: column;
          justify-content: space-between;

          .nickname {
            font-size: 14px;
            font-weight: 600;
            color: rgba(51, 51, 51, 0.5);
            margin-bottom: 5px;
          }

          .content {
            color: #333;
            line-height: 1.3;
            word-break: break-all;
          }
        }

        .meta {
          display: flex;
          align-items: center;
          font-size: 12px;
          color: #888;
          margin-top: 10px;

          .reply {
            cursor: pointer;
            color: #333;
            font-weight: 600;
            margin-left: 10px;
          }
        }
      }
    }
  }

  .children {
    margin: 20px 0 0 50px;

    .child-comment {
      margin-bottom: 20px;

      .comment-main {
        .avatar {
          width: 30px;
          height: 30px;
        }

        .comment-content {
          .name-content {
            height: auto;
            min-height: 40px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;

            .nickname {
              font-size: 14px;
              font-weight: 600;
              color: rgba(51, 51, 51, 0.5);
              margin-bottom: 5px;
            }

            .content {
              color: #333;
              line-height: 1.3;
            }
          }

          .meta {
            display: flex;
            align-items: center;
            font-size: 12px;
            color: #888;
            margin-top: 10px;
          }
        }
      }
    }
  }

}
</style>
